package com.zatech.cgnci.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.FlowTaskApprovalDTO;
import com.zatech.cgnci.project.dto.PaymentStatementExamineDTO;
import com.zatech.cgnci.project.dto.PaymentStatementSaveOrUpDTO;

import com.zatech.cgnci.project.dto.ReceiptPaymentStatementQueryDTO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciReceiptPaymentStatementService;

import com.zatech.cgnci.project.vo.CgnciClaimCasePageWorkbenchVO;
import com.zatech.cgnci.project.vo.CgnciReceiptPaymentStatementVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 收付费管理结算表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/api/receiptPaymentStatement")
@Api(produces = "application/json",tags = "收付费管理结算api接口文档")
public class CgnciReceiptPaymentStatementController {

    @Autowired
    private CgnciReceiptPaymentStatementService statementService;

    @PostMapping("/page")
    @ApiOperation(value = "分页列表查询")
    public IPage<CgnciReceiptPaymentStatementVO> page(@RequestBody ReceiptPaymentStatementQueryDTO queryDTO) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        IPage<CgnciReceiptPaymentStatementVO> page = statementService.page(queryDTO);
        List<CgnciReceiptPaymentStatementVO> records = page.getRecords();
        if (VerifyUtils.isNotEmpty(records)) {
            records.forEach(record -> record.setHasEdit(VerifyUtils.isNotEmpty(record.getCurrentApproverAndName()) && record.getCurrentApproverAndName().contains(user.getNameAndCode())));
        }
        return page;
    }

    @GetMapping("/details")
    @ApiOperation(value = "查看详情")
    public CgnciReceiptPaymentStatementVO queryDetailsByNumber(@RequestParam(value = "statementNumber") @NotNull(message = "statementNumber不能为空") Long statementNumber) {
        return statementService.queryDetailsByNumber(statementNumber);
    }

    @PostMapping("/next/create")
    @ApiOperation(value = "新增收付款")
    @Transactional(rollbackFor = Exception.class)
    public void nextCreate(@RequestBody @Valid PaymentStatementSaveOrUpDTO saveOrUpDTO){
        statementService.saveOrUpdate(saveOrUpDTO);
    }

    @PostMapping("/approve")
    @ApiOperation(value = "审核任务")
    @Transactional(rollbackFor = Exception.class)
    public void approve(@RequestBody @Valid PaymentStatementExamineDTO flowTaskDTO) {
        statementService.approve(flowTaskDTO);
    }
}

